import asyncio
import json
from fastmcp import FastMCP, Client
from config import (
    my_city,
    my_keywords,
    my_address,
    my_days,
    my_destination,
    my_origin,
    my_data_weather,
)
from ref_client import (
    ref_maps_weather,
    ref_maps_geo,
    ref_maps_text_search,
    ref_maps_distance_driving,
    ref_maps_distance_walking,
    ref_maps_distance_direct,
    ref_maps_direction_driving,
    ref_maps_direction_walking,
    ref_maps_direction_bicycling,
    ref_maps_direction_transit_integrated,
)
import segno
from dotenv import load_dotenv
from render import render_whole_html
import os
import asyncio
from llm_util import llm_gen_weather_tips, llm_gen_travel_tips

mcp = FastMCP("amap-tour-guide 🚀")

# # Only for Test
# @mcp.tool()
# def greet(name: str) -> str:
#     """Politely greet the customer"""
#     return f"Hello, {name}!"

# # Only for Test
# @mcp.tool()
# def add(a: int, b: int) -> int:
#     """Add two numbers"""
#     return a + b

# # Only for Test
# @mcp.tool()
# async def gen_weather(city:str='北京', days:int=3):
#     """返回天气信息"""
#     # with Client(ref_mcp_server_url) as ref_client:
#     #     result = ref_client.call_tool("maps_weather", {"city": city})
#     #     print(result)
#     result = await ref_maps_weather(city)
#     return result

@mcp.tool()
async def gen_tour_tips(city:str=my_city, days:int=3)->str:
    """Generate an html page of tour tips"""
    real_data_weather_result = await ref_maps_weather(city)
    print('real_data_weather_result: \n'+str(real_data_weather_result))
    real_data_weather = json.loads(real_data_weather_result[0].text)
    print('real_data_weather: \n'+str(real_data_weather))

    real_weather_tips = llm_gen_weather_tips(real_data_weather)
    real_text_search_result = await ref_maps_text_search(city)
    print(f'real_text_search_result: \n{real_text_search_result}')
    real_text_search = json.loads(real_text_search_result[0].text)['pois']
    print(f'real_text_search: \n{real_text_search}')

    real_travel_input_info = {
        "city": city,
        "days": days,
        "candidate_places": [{'name':position_dict['name'], 'address':position_dict['address']} for position_dict in real_text_search],
        "forecasts": real_data_weather['forecasts']
    }
    real_travel_tips = llm_gen_travel_tips(travel_input_info=real_travel_input_info)

    # real_data_text_search = await ref_maps_text_search(city)
    result = render_whole_html(data_weather=real_data_weather, weather_tips=real_weather_tips, travel_tips=real_travel_tips, text_search_result=real_text_search, city=city)
    return result

# @mcp.tool()
# def gen_qrcode(url: str, name:str='my_qrcode', format:str='png') -> str:
#     """Generate a QR code"""
#     qrcode = segno.make(url)
#     qrcode_filename = name + '.' + format
#     qrcode.save(qrcode_filename) # output the qrcode
#     return qrcode_filename

def main():
    # mcp.run(transport="streamable-http") # This will start a Uvicorn server on the default host (127.0.0.1), port (8000), and path (/mcp).
    mcp.run( # or: customize your MCP server's port and path
        # transport="streamable-http",
        transport="sse",
        host="0.0.0.0",
        port=8000,
        path="/mcp",
        log_level="debug",
    )

# # or: Use run_async() in async contexts
# async def main():
#     await mcp.run_async(transport="streamable-http")

if __name__ == "__main__":
    main()
    # pass
